package com.inno.auth.domain.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.inno.auth.common.entity.PageResult;
import com.inno.auth.common.enums.FeedbackNotifyTypeEnum;
import com.inno.auth.common.enums.FeedbackStatusEnum;
import com.inno.auth.common.enums.FeedbackTypeEnum;
import com.inno.auth.common.enums.YesOrNoEnum;
import com.inno.auth.common.utils.DateUtils;
import com.inno.auth.domain.convert.FeedbackConverter;
import com.inno.auth.domain.entity.CreateNewsMessageBO;
import com.inno.auth.domain.entity.FeedbackBO;
import com.inno.auth.domain.service.FeedbackDomainService;
import com.inno.auth.infra.basic.entity.PlannerFeedbackInfo;
import com.inno.auth.infra.basic.service.PlannerFeedbackInfoService;
import jakarta.annotation.Resource;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author sean
 */
@Service
public class FeedbackDomainServiceImpl implements FeedbackDomainService {
    @Resource
    private PlannerFeedbackInfoService plannerFeedbackInfoService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public void submit(FeedbackBO feedbackBO) {
        PlannerFeedbackInfo plannerFeedbackInfo = FeedbackConverter.INSTANCE.convertBOToEntity(feedbackBO);
        plannerFeedbackInfo.setUserId(feedbackBO.getCurrentUser());
        plannerFeedbackInfo.setCreateTime(DateUtils.getNowDate());
        plannerFeedbackInfo.setId(IdUtil.getSnowflakeNextId());
        String images = feedbackBO.getImages();
        String[] imageArr = StringUtils.split(images, ",");
        val imageJsonStr = JSON.toJSONString(imageArr);
        plannerFeedbackInfo.setImages(imageJsonStr);
        plannerFeedbackInfo.setStatus(FeedbackStatusEnum.WAIT.getCode());
        plannerFeedbackInfo.setIsProcess(YesOrNoEnum.NO.getCode());
        this.plannerFeedbackInfoService.insert(plannerFeedbackInfo);
    }

    @Override
    public PageResult<FeedbackBO> queryPage(FeedbackBO feedbackBO) {
        PlannerFeedbackInfo plannerFeedbackInfo = FeedbackConverter.INSTANCE.convertBOToEntity(feedbackBO);
        PageHelper.startPage(feedbackBO.getPage(), feedbackBO.getPageSize());
        Page<PlannerFeedbackInfo> entityPage = this.plannerFeedbackInfoService.queryPage(plannerFeedbackInfo);
        if (CollectionUtil.isEmpty(entityPage)) {
            return PageResult.of(Collections.emptyList(), 0L);
        }

        List<PlannerFeedbackInfo> entityResult = entityPage.getResult();


        List<FeedbackBO> boList = FeedbackConverter.INSTANCE.convertEntityListToBOList(entityResult);

        for (FeedbackBO bo : boList) {
            bo.setStatus(FeedbackStatusEnum.getDescByCode(feedbackBO.getStatus()));
            bo.setType(FeedbackTypeEnum.getDescByCode(bo.getType()));
            String images = bo.getImages();
            bo.setImageUrls(JSON.parseArray(images, String.class));
        }
        return PageResult.of(boList, entityPage.getTotal());
    }

    @Override
    public void processFeedback(FeedbackBO feedbackBO) {
        PlannerFeedbackInfo plannerFeedbackInfo = this.plannerFeedbackInfoService.queryById(feedbackBO.getId());
        if (!FeedbackStatusEnum.WAIT.getCode().equals(plannerFeedbackInfo.getStatus())
                || YesOrNoEnum.YES.getCode().equals(plannerFeedbackInfo.getIsProcess())) {
            return;
        }
        String notifyType = feedbackBO.getNotifyType();
        String processCause = feedbackBO.getProcessCause();
        String status = feedbackBO.getStatus();
        plannerFeedbackInfo.setIsProcess(YesOrNoEnum.YES.getCode());
        plannerFeedbackInfo.setStatus(status);
        plannerFeedbackInfo.setProcessCause(processCause);


        // 处理通知用户的逻辑
        if (FeedbackNotifyTypeEnum.NOTIFY_ANNOUNCEMENT.getCode().equals(notifyType)) {
            // 发布公告  mq消息
            CreateNewsMessageBO message = new CreateNewsMessageBO();
            message.setContent(processCause);
            message.setTitle("意见反馈处理通知");
            rocketMQTemplate.convertAndSend("createNews",message);
        } else if (FeedbackNotifyTypeEnum.NOTIFY_USER.getCode().equals(notifyType)) {
            // 通知用户
            // TODO 暂时不实现 需要有短信模板

        }
        this.plannerFeedbackInfoService.update(plannerFeedbackInfo);
    }

    private String buildNotifyContent(Date feedbackTime, String status, String processCause, String type) {
        String result = StringUtils.EMPTY;
        if (FeedbackStatusEnum.REPAIR.getCode().equals(status)) {
            // 已修复
            result += "尊敬的用户 您于 " + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, feedbackTime)
                    + "反馈的" + FeedbackTypeEnum.getDescByCode(type)
                    + "已修复，工作人员反馈意见: " + processCause;
            ;
        } else if (FeedbackStatusEnum.REJECT.getCode().equals(status)) {
            result += "尊敬的用户 您于 " + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, feedbackTime)
                    + "反馈的" + FeedbackTypeEnum.getDescByCode(type)
                    + "已拒绝，工作人员反馈意见: " + processCause;
        }
        return result;
    }
}
